package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacRoleExp;
import com.db.compare.model.RbacRole;
import com.db.compare.mapper.RbacRoleMapper;
import com.db.compare.service.RbacRoleService;
import com.db.compare.vo.RbacRoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    @Autowired
    private RbacRoleMapper rbacroleMapper;
    @Override
    public PageInfo<RbacRole> pageInfo(RbacRoleVo rbacroleVo) {
        PageHelper.startPage(rbacroleVo.getPageNum(),rbacroleVo.getPageSize());
        List<RbacRole> rbacroleList = rbacroleMapper.findList(rbacroleVo);
        PageInfo<RbacRole> pageInfo = new PageInfo<>(rbacroleList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "角色管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacRoleExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacRole> rbacroleList = rbacroleMapper.findByIds(arrays);
                List<RbacRoleExp> rbacroleExpList = new ArrayList<>();
                for(RbacRole rbacrole:rbacroleList){
                    RbacRoleExp rbacroleExp = new RbacRoleExp();
                    BeanUtils.copyProperties(rbacrole,rbacroleExp);
                    rbacroleExpList.add(rbacroleExp);
                }
                EasyExcel.write(outputStream, RbacRoleExp.class).sheet(fileName).doWrite(rbacroleExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacRoleExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacRoleExp.class,
                    importParams);
            List<RbacRoleExp> rbacroleExps = result.getList();
            for (RbacRoleExp rbacroleExp : rbacroleExps) {
                RbacRole rbacrole = new RbacRole();
                BeanUtils.copyProperties(rbacrole,rbacroleExp);
                int i = rbacroleMapper.insert(rbacrole);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
